package de.jmda.mview.proj;

import java.io.File;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.log4j.Logger;

import de.jmda.gui.swing.mvc.ModelBase;
import de.jmda.mproc.swingui.JPanelFileSetJavaSourceFilesModel;
import de.jmda.mproc.swingui.JPanelVisualProcessorModel;
import de.jmda.util.fileset.FileSetJavaFilesOnly;
import de.jmda.util.gui.swing.JPanelFavouritesModel;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ProjectModel extends ModelBase
{
	private final static Logger LOGGER = Logger.getLogger(ProjectModel.class);
	
	public final static String DEFAULT_NAME = "default project model";

	/**
	 * project name
	 */
	@XmlAttribute(required=true)
	private String name;
	
	/**
	 * model for panel with visual processing configuration
	 */
	@XmlElement
	private JPanelVisualProcessorModel visualProcessorModel;
	
	/**
	 * model for panel with diagrams
	 */
	@XmlElement
	private DiagramsModel diagramsModel;

	/**
	 * make JAXB happy
	 * <p>
	 * preserve this constructor for JAXB, do not call other constructors with
	 * default values
	 */
	private ProjectModel()
	{
		super();
	}

	public ProjectModel(
			String                     name,
			JPanelVisualProcessorModel visualProcessorModel,
			DiagramsModel              diagramsModel)
	{
		this();

		this.name                 = name;
		this.visualProcessorModel = visualProcessorModel;
		this.diagramsModel        = diagramsModel;
	}
	
	public String getName()
	{
		return name;
	}
	
	public JPanelVisualProcessorModel getVisualProcessorModel()
	{
		return visualProcessorModel;
	}

	public DiagramsModel getDiagramsModel()
	{
		return diagramsModel;
	}
	
	public static ProjectModel createDefaultProjectModel()
	{
		return new ProjectModel(
				DEFAULT_NAME,
				createDefaultVisualProcessorModel(),
				DiagramsModel.createDefaultDiagramsModel());
	}

	public static JPanelVisualProcessorModel createDefaultVisualProcessorModel()
	{
		// initialise jPanelVisualProcessorModel
		List<String> annotationProcessorClassNames = new LinkedList<String>();

		List<File> favourites = new LinkedList<File>();

		try
    {
      favourites.add(new File(new File(".").getCanonicalPath()));
    }
    catch (IOException e)
    {
    	LOGGER.error("failure getting canonical path for .", e);
    }

		return
				new JPanelVisualProcessorModel(
						"default visual processor model",
						annotationProcessorClassNames,
    				new JPanelFileSetJavaSourceFilesModel(
    						new FileSetJavaFilesOnly(),
    						new JPanelFavouritesModel(favourites)));
	}
}